<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>rest参数</h1>
    <h2>细节</h2>
    <ul>
        <li>rest参数只能放在形式参数的最后一个。</li>
        <li>使用了rest参数之后，可以不需要使用arguments对象</li>
    </ul>
    <script>
        // ...a：rest参数，自动将多个动态参数封装成数组对象
        function sum(...a) {
            let sum = 0;
            for (var v of a) {
                sum += v;
            }
            console.log("sum = ",sum);
        }   

        sum(1); // 1
        sum(1,2); // 3
        sum(1,2,3,4,5); // 15

        function eat(name,...food) {
            let str = `${name}吃了：`;
            for (var v of food) {
                str += `${v} `;
            }
            console.log(str);
        }

        eat("路飞","牛肉","猪肉","鸡肉","鸭肉");
    </script>
</body>
</html>